/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.manto;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.manto.ReunionForm;
import com.uca.sisbec.helpers.manto.ReunionHelper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;


/**
 *
 * @author Alex
 */
public class ReunionAction extends HibernateAction {
    
    public ActionForward init(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute("save", "new");
        return mapping.findForward("success");
    }

    public ActionForward initBusqueda(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        return mapping.findForward("search");
    }
    
    public ActionForward AgregarReunion(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ReunionForm rf = (ReunionForm) form;
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        ReunionHelper helper = new ReunionHelper(getSession(request));
        if (helper.ValidateReunion(rf) == false){
            int idR = 0;
            idR = helper.InsertarReunionDB(rf);
            rf.setIdReunion(idR);
            msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("reunion.nuevo"));
            request.setAttribute("msg", msg);
            request.setAttribute("save", "update");
        }else{
            //validar que no haya otra reunion a la misma hora, lugar, fecha y con el mismo tema
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("reunion.existe", rf.getTema()));
            request.setAttribute("errors", errors);
            request.setAttribute("save", "new");
        }
        return mapping.findForward("success");
    }
    
    public ActionForward BuscarReunion(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ReunionForm rf = (ReunionForm) form;
        ReunionHelper helper = new ReunionHelper(getSession(request));
        request.setAttribute("reunionData", helper.BuscarReunionDB(rf));
        return mapping.findForward("search");
    }
    
    public ActionForward SeleccionarReunion(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ReunionForm rf = (ReunionForm) form;
        ReunionHelper helper = new ReunionHelper(getSession(request));
        String id = Integer.toString(rf.getIdReunion());
        helper.BuscarReunionDB(id, rf);
        request.setAttribute("save", "update");
        return mapping.findForward("success");
    }
    
    public ActionForward ModificarReunion(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ReunionForm rf = (ReunionForm) form;
        ReunionHelper helper = new ReunionHelper(getSession(request));
        helper.ModificarReunionDB(rf);
        ActionMessages msg = new ActionMessages();
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("reunion.modificada"));
        request.setAttribute("msg", msg);
        request.setAttribute("save", "update");
        return mapping.findForward("success");
    }
    
    public ActionForward EliminarReunion(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ReunionForm rf = (ReunionForm) form;
        ReunionHelper helper = new ReunionHelper(getSession(request));
        helper.EliminarReunionDB(rf);
        ActionMessages msg = new ActionMessages();
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("reunion.eliminada"));
        request.setAttribute("msg", msg);
        request.setAttribute("save","new");
        rf.reset(mapping, request);
        return mapping.findForward("success");
    }
    
}
